package org.jeecg.modules.car.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.car.CarDerateDetails;
import org.jeecg.entity.car.CarMerchantDerate;
import org.jeecg.enums.car.CarDerateStatusEnum;
import org.jeecg.enums.PayStatusEnum;
import org.jeecg.modules.car.entity.CarMerchant;
import org.jeecg.modules.car.entity.CarMerchantOrder;
import org.jeecg.modules.car.mapper.CarDerateDetailsMapper;
import org.jeecg.modules.car.mapper.CarMerchantDerateMapper;
import org.jeecg.modules.car.mapper.CarMerchantMapper;
import org.jeecg.modules.car.mapper.CarMerchantOrderMapper;
import org.jeecg.modules.car.service.ICarMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 商家管理
 * @Author: jeecg-boot
 * @Date:   2020-10-17
 * @Version: V1.0
 */
@Service
public class CarMerchantServiceImpl extends ServiceImpl<CarMerchantMapper, CarMerchant> implements ICarMerchantService {

    @Autowired
    private CarMerchantMapper carMerchantMapper;

    @Autowired
    private CarMerchantOrderMapper carMerchantOrderMapper;

    @Autowired
    private CarMerchantDerateMapper carMerchantDerateMapper;

    @Autowired
    private CarDerateDetailsMapper carDerateDetailsMapper;



    @Override
    public List<CarMerchant> findPage(Page<CarMerchant> page, CarMerchant carMerchant) {
        carMerchant.setTenantId(SsoUtil.getLoginUser().getTenantId());
        return carMerchantMapper.findPage(page,carMerchant);
    }

    /**
     * 商户余额充值
     * @param payAmount 到账金额
     * @param actualAmount 实付金额
     * @param payType   支付类型
     */
    @Override
    @Transactional
    public Result merchantPay(CarMerchant carMerchant,BigDecimal payAmount, BigDecimal actualAmount, String remarks, String payType, String payMode) {
        Result<CarMerchant> result = new Result<CarMerchant>();
        //优惠金额  = 到账 - 实付
        BigDecimal discountAmount =payAmount.subtract(actualAmount);
       //设置订单字段
        CarMerchantOrder merchantOrder = new CarMerchantOrder();
        merchantOrder.setMerchantId(carMerchant.getId());
        merchantOrder.setTenantId(SsoUtil.getLoginUser().getTenantId());
        merchantOrder.setProjectId(carMerchant.getProjectId());
        merchantOrder.setSn(StringUtils.createSn(carMerchant.getProjectCode(), 3));
        merchantOrder.setPayType(payType);
        merchantOrder.setPayMode(payMode);
        merchantOrder.setPayAmount(actualAmount);
        merchantOrder.setChargeStatus(1);
        merchantOrder.setDiscountAmount(discountAmount);
        merchantOrder.setActualAmount(payAmount);
        merchantOrder.setPayTime(new Date());
        merchantOrder.setPayStatus(PayStatusEnum.PAY_STATUS_YES.getCode());
        merchantOrder.setRemarks(remarks);
        merchantOrder.setBalance(payAmount.add(carMerchant.getRemainAmount()));
        merchantOrder.setCreateBy(SsoUtil.getLoginUser().getUsername());
        //账户余额 =  支付金额 + 优惠金额 + 剩余额度
        carMerchant.setRemainAmount(payAmount.add(carMerchant.getRemainAmount()));
        //更新商户余额
        carMerchantMapper.updateById(carMerchant);
        //添加充值记录
        carMerchantOrderMapper.insert(merchantOrder);
        return result.success("充值成功");
    }

    @Override
    public CarMerchant getUserByName(String username) {
        return carMerchantMapper.getUserByName(username);
    }

    @Override
    public void updateRemainAmount(CarMerchant carMerchant) {
        carMerchantMapper.updateRemainAmount(carMerchant);
    }

    @Override
    public List<CarMerchant> getMerchantByParkList(String parkId) {
        return carMerchantMapper.getMerchantByParkList(parkId);
    }

    @Override
    public int updatePwd(String id, String newPwd,String remarks) {
        return carMerchantMapper.updatePwd(id,newPwd,remarks);
    }

    @Override
    public CarMerchant getSelfById(String merchantId) {
        return carMerchantMapper.getSelfById(merchantId);
    }

    /**
     * 月底定时清空赠送余额
     */
    @Override
    public void clearAllDonateAmount() {
        carMerchantMapper.updateDonate();
        CarMerchantDerate carMerchantDerate = new CarMerchantDerate();
        carMerchantDerate.setEndTime(new Date());
        carMerchantDerate.setRemarks("赠送金额过期作废");
        carMerchantDerate.setStatus(CarDerateStatusEnum.CAR_DERATE_STATUS_MISS.getCode());
        carMerchantDerateMapper.deleteDonateDerate(carMerchantDerate);
    }

    @Override
    public Result<String> addAllDonateAmount() {
        return carMerchantMapper.giveAllDonateAmonut();
    }

    @Override
    @Transactional
    public Result merchantDonateRecharge(CarMerchant carMerchant, double payAmount, String payMode, String remarks) {
        Result<CarMerchant> result = new Result<CarMerchant>();
        //支付金额
        BigDecimal amount = new BigDecimal(payAmount);
        //设置订单字段
        CarMerchantOrder merchantOrder = new CarMerchantOrder();
        merchantOrder.setMerchantId(carMerchant.getId());
        merchantOrder.setTenantId(SsoUtil.getLoginUser().getTenantId());
        merchantOrder.setProjectId(carMerchant.getProjectId());
        merchantOrder.setSn(StringUtils.createSn(carMerchant.getProjectCode(), 3));
        merchantOrder.setPayMode(payMode);
        merchantOrder.setPayAmount(amount);
        merchantOrder.setOrderType(1); // 积分订单
        merchantOrder.setChargeStatus(1);
        merchantOrder.setDiscountAmount(new BigDecimal(0));
        merchantOrder.setActualAmount(amount);
        merchantOrder.setPayTime(new Date());
        merchantOrder.setPayStatus(PayStatusEnum.PAY_STATUS_YES.getCode());
        merchantOrder.setRemarks(remarks);
        merchantOrder.setBalance(amount.add((carMerchant.getDonateAmount())));
        merchantOrder.setCreateBy(SsoUtil.getLoginUser().getUsername());
        //积分余额 =  充值金额 + 剩余额度
        carMerchant.setDonateAmount(amount.add(carMerchant.getDonateAmount()));
        //更新商户余额
        carMerchantMapper.updateById(carMerchant);
        //添加充值记录
        carMerchantOrderMapper.insert(merchantOrder);
        return result.success("充值成功");
    }

    @Override
    public List<CarDerateDetails> findDiscountListByMerChantById(Page<CarDerateDetails> page, CarMerchant carMerchant) {
        return carMerchantOrderMapper.findDiscountListByMerChantById(page, carMerchant);
    }
}
